<?php
declare(strict_types=1);
namespace Swb\Shop\Service;

use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\Utils\Context;
use Swb\Shop\Helper\ShopTrait;
use Swb\Shop\Model\TShop;
use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\ApplicationContext;
use Swb\Shop\Contract\CommonServiceInterface;

use Swb\Shop\Model\SysDataDict;
use Swb\Shop\Model\SysDataDictGroup;
use Swb\Shop\Model\SysProvinceCity;

class CommonService implements CommonServiceInterface
{
    public function getArea($area_id = 610100)
    {
        return SysProvinceCity::query()->with('area')->where('del_flag', 1)->where(
            'parent_id',
            $area_id
        )->select('name', 'id')->orderBy('sort')->get();
    }

    public function getDict($code)
    {
        $code    =explode(',', $code);
        $group_id=SysDataDictGroup::query()->whereIn('code', $code)->selectRaw('id,code,name')->get();
        $arr     =[];
        if ($group_id) {
            foreach ($group_id as $key => $item) {
                $data              =SysDataDict::query()->selectRaw('code,name,id,/*level,*/use_value')->where(
                    'group_id',
                    $item['id']
                )->where('del_flag', 1)/*->orderBy('level')*/->get();
                $arr[$key]['code'] =$item['code'];
                $arr[$key]['name'] =$item['name'];
                $arr[$key]['id']   =$item['id'];
                $arr[$key]['group']=$data??[];
            }
        }
        return ['code'=>200, 'data'=>$arr];
    }
}